package fragment.contact;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.menhoo.toollibrary.common.utils.StringUtil;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import application.HttpUrlConstant;
import application.UIApplication;
import custom.PinnedSectionListView;
import menhoo.com.i8officelibrary.R;
import model.EmployeeItemDBModel;

/**
 * Created by ThinkPad on 2017/5/23.
 */

public class ContactsAdapter  extends BaseAdapter implements PinnedSectionListView.PinnedSectionListAdapter {

    private boolean isEdit;

    private Context context;
    private LayoutInflater inflater;
    private List<EmployeeItemDBModel> datas = new ArrayList<EmployeeItemDBModel>();
    private BitmapUtils bitmapUtils;

    public void setEdit(boolean isEdit){
        this.isEdit = isEdit;
    }

    public ContactsAdapter(Context context, List<EmployeeItemDBModel> list, boolean edit) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        bitmapUtils = new BitmapUtils(context, UIApplication.getAppCachePath(context));
        isEdit = edit;
        int sectionPosition = -1, listPosition = 0;
        for (EmployeeItemDBModel section : list) {
            if (section.type == EmployeeItemDBModel.SECTION) {
                sectionPosition++;
                onSectionAdded(section, sectionPosition);
            }
            section.sectionPosition = sectionPosition;
            section.listPosition = listPosition++;

        }
        datas = list;
    }

    @Override
    public int getCount() {
        if(datas!=null && datas.size()>0){
            return datas.size();
        }else {
            return 0;
        }
    }

    @Override
    public EmployeeItemDBModel getItem(int position) {
        if(position < 0 || position >= datas.size())
            return null;
        else
            return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void clearSelected() {
        for (EmployeeItemDBModel item : datas) {
            item.checked = false;
        }
    }

    public void SelectedAll() {
        for (EmployeeItemDBModel item : datas) {
            item.checked = true;
        }
    }

    public List<EmployeeItemDBModel> getSelectData() {
        List<EmployeeItemDBModel> list_ = new ArrayList<EmployeeItemDBModel>();
        for (EmployeeItemDBModel item : datas) {
            if (item.checked == true && item.type == EmployeeItemDBModel.ITEM) {
                list_.add(item);
            }
        }
        if (list_.size() > 0) {
            return list_;
        } else {
            return null;
        }
    }

    public List<EmployeeItemDBModel> getAllData() {
        if (datas.size() > 0) {
            return datas;
        } else {
            return null;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int index = position;
        EmployeeItemDBModel item = getItem(position);

        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            if (item.type == EmployeeItemDBModel.ITEM) {
                convertView = inflater.inflate(R.layout.adapter_item_address_book, null);
                viewHolder.img = (ImageView) convertView.findViewById(R.id.imageView1);
                viewHolder.checkBox = (ImageView) convertView.findViewById(R.id.imageViewCheck);
                viewHolder.dept = (TextView) convertView.findViewById(R.id.textView2);
            } else {
                convertView = inflater.inflate(R.layout.adapter_item_address_book_only_text, null);
            }
            viewHolder.content = (TextView) convertView.findViewById(R.id.textView1);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (item.type == EmployeeItemDBModel.ITEM  ) {
            //设置图片
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) viewHolder.img.getLayoutParams();
            lp.setMargins(10, 8, 0, 8);
            viewHolder.img.setLayoutParams(lp);
            ((View) viewHolder.img.getParent()).setBackgroundResource(R.drawable.selector_list);

            if(!TextUtils.isEmpty(item.DeptID) && item.DeptID.equalsIgnoreCase("chat")){
                viewHolder.img.setImageResource(R.mipmap.im_icon_se);
            }else if(!TextUtils.isEmpty(item.DeptID) && item.DeptID.equalsIgnoreCase("all")){
                viewHolder.img.setImageResource(R.mipmap.department_img);
            }else{
                Map<String, String> parm = new HashMap<String, String>();
                parm.put("emplid", item.EmplID);
                bitmapUtils.display(viewHolder.img, HttpUrlConstant.getWebSiteFullUrl(HttpUrlConstant.doShowPhoto, parm));
            }

            if (isEdit && !TextUtils.isEmpty(item.EmplID) ) {
                viewHolder.checkBox.setVisibility(View.VISIBLE);
                if (getItem(index).checked) {
                    viewHolder.checkBox.setImageResource(R.drawable.selecter_selected_icon);
                } else {
                    viewHolder.checkBox.setImageResource(R.drawable.selecter_unselected_icon);
                }
            } else {
                viewHolder.checkBox.setVisibility(View.GONE);
            }
        }

        viewHolder.content.setText(item.Name);
        if (viewHolder.dept != null) {
            if (StringUtil.isEmpty(item.DeptAndPosStr)) {
                viewHolder.dept.setVisibility(View.GONE);
            } else {
                viewHolder.dept.setVisibility(View.VISIBLE);
                viewHolder.dept.setText(item.DeptAndPosStr);
            }
        }
        return convertView;
    }

    protected void onSectionAdded(EmployeeItemDBModel section, int sectionPosition) {
    }

    @Override
    public int getItemViewType(int position) {
        EmployeeItemDBModel item = getItem(position);
        if( item ==  null ){
            return 0;
        }else {
            return getItem(position).type;
        }
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
        return viewType == EmployeeItemDBModel.SECTION;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }
    public void changeData(List<EmployeeItemDBModel> addData) {
        initGetData(addData);
        notifyDataSetChanged();
    }

    private void initGetData(List<EmployeeItemDBModel> list){

        int sectionPosition = -1, listPosition = 0;
        for (EmployeeItemDBModel section : list) {
            if (section.type == EmployeeItemDBModel.SECTION) {
                sectionPosition++;
            }
            section.sectionPosition = sectionPosition;
            section.listPosition = listPosition++;

        }
        datas.clear();
        datas.addAll(list);
    }

    public class ViewHolder {
        TextView content, dept;
        ImageView img;
        LinearLayout linearLayout;
        ImageView checkBox;
    }
}